package com.ghh.jaxws;

import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.w3c.dom.Document;

public class MyWSClient {
	private String	wsdlUrl	= "http://slcai713.us.oracle.com:9406/hcmCommonBatchLoader/LoaderIntegrationService?WSDL";
	private String	message	= "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
									+ "<soap:Body>"
									+ "<ns1:submitBatch xmlns:ns1=\"http://xmlns.oracle.com/apps/hcm/common/batchLoader/core/loaderIntegrationService/types/\">"
									+ "<ns1:ZipFileName>TESTDATA01</ns1:ZipFileName>"
									+ "<ns1:BusinessObjectList>PERSON</ns1:BusinessObjectList>"
									+ "<ns1:BatchName>TestDataBatch</ns1:BatchName>" 
									+ "<ns1:LoadType>FBL</ns1:LoadType>"
									+ "<ns1:AutoLoad>N</ns1:AutoLoad>" 
									+ "</ns1:submitBatch>" 
									+ "</soap:Body>"
									+ "</soap:Envelope>";
	private String	ns		= "http://xmlns.oracle.com/apps/hcm/common/batchLoader/core/loaderIntegrationService/";

	public void callService() {
		try {
			URL url = new URL(wsdlUrl);
			QName sname = new QName(ns, "LoaderIntegrationService");
			Service service = Service.create(url, sname);

			Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,
					"LoaderIntegrationServiceSoapHttpPort"), SOAPMessage.class, Service.Mode.MESSAGE);

			dispatch.getRequestContext().put(Dispatch.USERNAME_PROPERTY, "HCM_APP_ADMIN_ALL");
			dispatch.getRequestContext().put(Dispatch.PASSWORD_PROPERTY, "Welcome1");
			Properties p = new Properties();
			// 创建SOAPMessage
			// SOAPMessage msg = MessageFactory.newInstance().createMessage();
			// SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
			// SOAPBody body = envelope.getBody();

			// 创建QName来指定消息中传递数据
			// QName ename = new QName(ns, "add", "nn");
			// // <nn:add xmlns="xx"/>
			// SOAPBodyElement ele = body.addBodyElement(ename);
			// ele.addChildElement("firstA").setValue("22");
			// ele.addChildElement("SecondB").setValue("33");
			// msg.writeTo(System.out);
			// System.out.println("\n invoking....");

			MimeHeaders header = null;
			SOAPMessage msg = MessageFactory.newInstance().createMessage(header,
					new ByteArrayInputStream(message.getBytes()));

			// 通过Dispatch传递消息,会返回相应消息
			SOAPMessage response = dispatch.invoke(msg);
			response.writeTo(System.out);
			System.out.println();

			// 将相应的消息转换为doc对象
			Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
			String str = doc.getElementsByTagName("submitBatchResult").item(0).getTextContent();
			System.out.println(str);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new MyWSClient().callService();
	}
}
